package com.example.administrator.managesystem.presenter.profile;

import android.content.Context;
import android.util.Log;

import com.example.administrator.managesystem.bean.HeadChangeBean;
import com.example.administrator.managesystem.bean.ProfileChangeBean;
import com.example.administrator.managesystem.bean.PsdSettingBean;
import com.example.administrator.managesystem.network.GuideAPI;
import com.example.administrator.managesystem.network.ServiceManager;
import com.example.administrator.managesystem.ui.ProfileView;
import com.example.administrator.managesystem.utils.ToastUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ProfileChangePresenter {
    private ProfileView profileView;

    public ProfileChangePresenter(ProfileView profileView) {
        this.profileView = profileView;
    }

    public void profileChangeRequest(String user_id, String token, final String name, String sex, String province, String city, String area, String age, String address, String remarks, List<String> list, final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).ProfileChangeRequest(user_id,token,name,sex,province,city,area,age,address,remarks,list)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<ProfileChangeBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(ProfileChangeBean profileChangeBean) {
                       if(profileChangeBean.getStatus()==200){
                           ToastUtils.showToast(context,profileChangeBean.getData());
                           profileView.navigateToHome(name);
                       }else{
                           ToastUtils.showToast(context,profileChangeBean.getMsg());
                       }

                    }

                });

    }
    public void headChangeRequest(String user_id, String token, final String url , final Context context){

        final File file = new File(url);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

        // MultipartBody.Part  和后端约定好Key，这里的partName是用image
        MultipartBody.Part body = MultipartBody.Part.createFormData("head_img", file.getName(), requestFile);

        ServiceManager.getInstance(context).getService(GuideAPI.class).headSetting(user_id,token,body)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<HeadChangeBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(HeadChangeBean headChangeBean) {
                     if(headChangeBean.getStatus()==200){
                         ToastUtils.showToast(context,headChangeBean.getData());
                         profileView.showMessage(file,url);
                     }

                    }

                });

    }

}
